// 03admin/03waiter/agreeTo/agreeTo.js

var co = require('../../../../resource/js/common.js')
var app = getApp()
var _this, _data

var _inputPhone, _Sms_Code
var _unionid = wx.getStorageSync('unionid')
var _session
var _openid
var _opt = {}
//是否取消倒计时
var isCancelCount = 0

Page({
   data: {
      Biz_ID: '', //店铺ID
      user: 0, //用户信息
      Verified_Phone: '', //验证的手机号,初始为0，自动登录后变为非0，以确认要否显示微信授权
      getSmsCodeText: '获取验证码', //验证码
      Verified_Ok: '', //验证正确否
      isCode: 0, //验证码0错误，1正确
   },

   onLoad: function(opt) {
      _this = this
      _data = this.data

      //判断参数
      if (co.empty(opt.scene)) {
         co.Modal('缺少“opt.scene”参数')
         return
      }
      if (opt.scene.indexOf("BizID.") == -1) {
         co.Modal('“opt.scene”参数缺少“BizID.”')
         return
      }
      console.log('opt======', opt)
      _opt = app.do_opt_scene_data(opt)
      console.log('_opt======',_opt)
      //获取session值
      // _session = wx.getStorageSync('session')
      // console.log('_session========', _session)
      // if (!co.empty(_session.unionid) || !co.empty(_session.openid)) {
         //自动登录
      _this.autoLogin()
      // } else {
      //    app.wxLogin(function(res) {
      //       console.log("【app.wxLogin登录结果】===", res)
      //       //自动登录
      //       _this.autoLogin()
      //    })
      // }
   },

   //自动登录
   autoLogin: function() {
      //获取session值
      _session = _session ? _session : wx.getStorageSync('session')
      //参数
      var url = 'user/Mg/autoLogin'
      var param = {
         // unionid: _session.unionid,
         openid: _session.openid,
         appid: app.Data.app.appid
      }
      co.getShareApi(url, param, function(res) {
         console.log('res===', res)
         //处理自动登录的用户数据
         _this.doUserData(res.data)
      })
   },

   //处理自动登录的用户数据
   doUserData: function(data) {
      console.log('data====', data)
      //获取用户信息
      var userInfo = data.userInfo
      app.Data.userInfo = userInfo
      //刷新页面
      _this.setData({
         user: userInfo,
         Verified_Ok: _this.checkPhone(userInfo.User_Mobile),
         Verified_Phone: userInfo.User_Mobile
      })
      //验证通过(已经是会员,并且有电话号码了)
      if (_data.Verified_Ok) {
         //验证是否在该商家已经成了服务员
         _this.isWaiter(data.biz_list)
      }
      console.log('app.Data.userInfo===', app.Data.userInfo)
   },

   //查看是否存在这个服务员
   isWaiter: function(bizList) {
      var url = 'waiter/Wmg/isWaiter'
      //参数
      var param = {
         User_ID: app.Data.userInfo.User_ID,
         Biz_ID: _opt.BizID
      }
      co.getShareApi(url, param, function(res) {
         if (res.code === 0) {
            //提示信息
            co.confirm('您已加入', '', '确定', function(_res) {
               _this.gotoHome()
            })
         }
      })
   },

   //选择商家店铺
   gotoHome: function() {
      // var len = bizList.length
      // //循环
      // for (var i = 0; i < len; i++) {
      //    var shop = bizList[i]
      //    //判断商家ID
      //    if (shop.Biz_ID == _opt.BizID) {
      //       app.Data.shopInfo = shop
      //       wx.switchTab({
      //          url: '/02homePage/01home/home',
      //       })
      //       console.log('app.Data.shopInfo===', app.Data.shopInfo)
      //       return;
      //    }
      // }
      wx.switchTab({
         url: '/01wasteRecovery/wasteRecovery',
      })
   },

   //添加服务员
   save: function(e) {
      var name = e.detail.value.name
      console.log('_session=====', _session)
      if (co.empty(name)) {
         co.toastAllText('姓名不能为空, 请重新输入')
         return
      }
      //参数
      var param = {
         Biz_ID: _opt.BizID,
         User_ID: app.Data.userInfo.User_ID,
         unionid: app.Data.userInfo.unionid,
         name: name
      }
      //添加服务员
      co.getShareApi('waiter/Wmg/save', param, function(res) {
         if (res.code == 0) {
            co.confirm('您已加入', '', '确定', function(_res) {
               _this.gotoHome()
            })
         } else {
            co.toastAllText(res.msg)
         }
      })
   },

   //一键获取微信认证手机
   getPhoneNumber: function(e) {
      app.decryptWxInfoData(e.detail, function(res) {
         _this.setData({
            Verified_Ok: 1,
            Verified_Phone: res.phoneNumber
         })
      })
   },

   //手机输入框事件
   inputPhone: function(e) {
      _inputPhone = e.detail.value
      console.log('【inputPhone(e)】===' + _inputPhone)
   },

   //检查手机格式
   checkPhone: function(phone) {
      var pho = phone ? phone : _inputPhone
      var rule = /^1[0-9]{10}$/
      console.log('【checkPhone(phone)==】' + pho)
      if (rule.test(pho)) {
         return 1
      }
      return 0
   },

   //换个手机
   changePhone: function(e) {
      _this.setData({
         Verified_Ok: 0,
         Verified_Phone: ''
      })
   },

   //输入验证码
   inputCode: function(e) {
      console.log('输入码==' + e.detail.value)
      if (_Sms_Code == e.detail.value) {
         //取消倒计时
         isCancelCount = 1
         //刷新页面
         _this.setData({
            Verified_Ok: 1,
            Verified_Phone: _data.Verified_Phone
         })
      }
   },

   //获取短信验证码
   getPhoneCode: function() {
      if (!_this.checkPhone(_inputPhone)) {
         co.toastAllText('手机号格式不正确')
         return
      }
      _data.Verified_Phone = _inputPhone
      // _this.setData({
      //    Verified_Phone: _inputPhone
      // })
      co.getShareApi('api/Sms/getPhoneCode', {
         mobile: _inputPhone,
         type: 'user_reg'
      }, function(res) {
         if (res.code == 0) {
            //获取验证码
            _Sms_Code = res.data.Sms_Code
            //倒计时
            var second = 60
            _this.setData({
               getSmsCodeText: '重新获取(' + second + '秒)'
            })
            _this.countDown(second)
         } else {
            co.toastAllText(res.msg)
         }
      })
   },

   //倒计时
   countDown: function(time) {
      var second = ''
      setTimeout(function() {
         if (!isCancelCount && time > 0) {
            time--
            second = '(' + time + '秒)'
            _this.setData({
               getSmsCodeText: '重新获取' + second
            })
            _this.countDown(time)
         } else {
            isCancelCount = 0
            _this.setData({
               getSmsCodeText: '重新获取' + second
            })
         }
      }, 1000);
   },

   //提示手机认证
   showToast: function() {
      co.toastAllText('请先手机认证')
   },

   //点击授权
   getWxUserInfo: function(e) {
      //获取session_key
      var session_key = wx.getStorageSync('session_key')
      //没有session_key
      if (co.empty(session_key)) {
         //请求登录
         app.wxLogin(function(res) {
            console.log("【app.wxLogin登录结果】===", res)
            //获取用户信息
            _this.getUserInfo(e.detail)
         })
         return
      }
      //获取用户信息
      _this.getUserInfo(e.detail)
   },

   //获取用户 或 注册 或更新
   getUserInfo: function(data) {
      if (co.empty(data.iv) || co.empty(data.encryptedData)) {
         return
      }
      //获取session_key
      var session_key = wx.getStorageSync('session_key')
      //参数
      var param = {
         appid: app.Data.app.appid,
         session_key: session_key,
         iv: data.iv,
         encryptedData: data.encryptedData,
         User_Mobile: _data.Verified_Phone,
         Owner_Id: _opt.InviterID
      }
      //获取用户信息
      co.getShareApi('user/Mg/getInfo', param, function(res) {
         if (res.code == 0) {
            //获取用户信息
            app.Data.userInfo = res.data
            //刷新页面
            _this.setData({
               user: res.data
            })
            console.log('app.Data.userInfo===', app.Data.userInfo)
         }
      })
   },

})